package com.anti.modular.sys.menu.dao.impl;

import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.menu.dao.SysMenuDao;
import com.anti.modular.sys.menu.entity.SysMenu;
import com.anti.modular.sys.menu.param.SysMenuParam;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-9
 */
@Repository
public class SysMenuDaoImpl implements SysMenuDao {
    @Resource
    DBUtils dbUtils;
    @Override
    public void add(SysMenu sysMenu) {
        String sql="INSERT INTO sys_menu (pid,pids, name, code, type, icon, router, " +
                "component, permission, application, open_type, visible, link, redirect, " +
                "weight, sort, remark, status, create_time, create_user) VALUES (?,?,?,?, ?, ?, ?, " +
                "?, ?, ?, ?, ?, ?, ?, " +
                "?, ?, ?, ?, ?, ?)";
        dbUtils.execSql(sql,sysMenu.getPid(),sysMenu.getPids(),sysMenu.getName(),sysMenu.getCode(),sysMenu.getType(),sysMenu.getIcon(),sysMenu.getRouter(),
                sysMenu.getComponent(),sysMenu.getPermission(),sysMenu.getApplication(),sysMenu.getOpenType(),sysMenu.getVisible(),sysMenu.getLink(),sysMenu.getRedirect(),
                sysMenu.getWeight(),sysMenu.getSort(),sysMenu.getRemark(),sysMenu.getStatus(),sysMenu.getCreateTime(),sysMenu.getCreateUser());
    }

    @Override
    public void delete(Long id) {
        String sql="update sys_menu set status=2 where id =?";
        dbUtils.execSql(sql,id);
    }

    @Override
    public void edit(SysMenu sysMenu) {
        String sql="update sys_menu set pid=?,name=?, code=?, type=?, icon=?, router=?, " +
                "component=?, permission=?, application=?, open_type=?, visible=?, link=?, redirect=?, " +
                "weight=?, sort=?, remark=?,update_time=?, update_user=? where id=?";
        dbUtils.execSql(sql,sysMenu.getPid(),sysMenu.getName(),sysMenu.getCode(),sysMenu.getType(),sysMenu.getIcon(),sysMenu.getRouter(),
                sysMenu.getComponent(),sysMenu.getPermission(),sysMenu.getApplication(),sysMenu.getOpenType(),sysMenu.getVisible(),sysMenu.getLink(),sysMenu.getRedirect(),
                sysMenu.getWeight(),sysMenu.getSort(),sysMenu.getRemark(),sysMenu.getUpdateTime(),sysMenu.getUpdateUser(),sysMenu.getId());
    }

    @Override
    public void updateBatchById(List<SysMenu> list) {
        String sql="update sys_menu set application=?,pids=? where id =?";
        Iterator<SysMenu> iterator = list.iterator();
        while (iterator.hasNext()){
            SysMenu next = iterator.next();
            dbUtils.execSql(sql,next.getApplication(),next.getPids(),next.getId());
        }
    }
}
